
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author FFK
 */
public class playmusic extends Thread implements PlayerListener {
    public  Player player = null;
    
    
    private VolumeControl volum;
    public String nhac;
    public playmusic(String _nhac){
         nhac = _nhac;
         try {
                 InputStream in = getClass().getResourceAsStream("/"+nhac);
                 String ctype=gettype(nhac);
                 player = Manager.createPlayer(in,ctype);
                 player.prefetch();
                 player.realize();
            } catch (Exception ex) {

            }
    }
        public void start(){
        }
        public  void play(String _nhac){
            nhac = _nhac;
            try {
                 InputStream in = getClass().getResourceAsStream("/"+nhac);
                 String ctype=gettype(nhac);
                 player = Manager.createPlayer(in,ctype);
                 player.setLoopCount(1);
                 player.prefetch();
                 player.realize();
                 player.start();
            } catch (Exception ex) {

            }
        }
        public void setloop(){
            player.setLoopCount(1);
        }
        public void stop(){
            try {
                player.stop();
            } catch (Exception ex) {

            }
        }
        
        public void close(){
            try {
                player.close();
                player = null;
            } catch (Exception ex) {

            }
        }
        
         private static String gettype(String url)
        throws Exception {
        String type;

        if (url.endsWith("wav")) {
            type = "audio/x-wav";
        } else if (url.endsWith("jts")) {
            type = "audio/x-tone-seq";
        } else if (url.endsWith("mid")) {
            type = "audio/midi";
        } else {
            throw new Exception("Cannot guess content type from URL: " + url);
        }

        return type;
    }

    public void playerUpdate(Player player, String string, Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
